<%@page import="org.lzy.pt.ContactManager"%>
<%@page import="org.lzy.pt.bean.Contact"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.List"%>
<%@ page import="org.jivesoftware.util.LocaleUtils,
                 org.jivesoftware.util.ParamUtils"
%><%@ page import="org.jivesoftware.util.StringUtils"%>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Collection" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<%!
    final int DEFAULT_RANGE = 100;
    final int[] RANGE_PRESETS = {25, 50, 75, 100, 500, 1000, -1};
%>

<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager"  />
<% webManager.init(request, response, session, application, out ); %>
<%  // Get parameters
%>

<html>
    <head>
        <title><fmt:message key="item.contact.summary"></fmt:message></title>
        <meta name="pageID" content="contact-summary"/>
    </head>
    <body>
<%  // Get parameters
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",webManager.getRowsPerPage("contact-summary", DEFAULT_RANGE));

    if (request.getParameter("range") != null) {
        webManager.setRowsPerPage("contact-summary", range);
    }

    // Get the user manager
    long userCount = ContactManager.getInstance().getCount();

    // paginator vars
    int numPages = (int)Math.ceil((double)userCount/(double)range);
    int curPage = (start/range) + 1;
%>

<p>
<fmt:message key="contact.summary.total_contact" />:
<b><%= LocaleUtils.getLocalizedNumber(userCount) %></b> --

<%  if (numPages > 1) { %>

    <fmt:message key="global.showing" />
    <%= LocaleUtils.getLocalizedNumber(start+1) %>-<%= LocaleUtils.getLocalizedNumber(start+range > userCount ? userCount:start+range) %>,

<%  } %>
-- <fmt:message key="contact.summary.contacts_per_page" />:
<select size="1" onchange="location.href='contact-summary.jsp?start=0&range=' + this.options[this.selectedIndex].value;">

    <% for (int aRANGE_PRESETS : RANGE_PRESETS) { %>

    <option value="<%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>"
            <%= (aRANGE_PRESETS == range ? "selected" : "") %>><%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>
    </option>

    <% } %>

</select>
</p>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="contact-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        int i;
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="contact-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="contact-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
    <tr>
        <th>&nbsp;</th>
		<th nowrap><fmt:message key="contact.username" /></th>
		<th nowrap><fmt:message key="contact.T_DateTime" /></th>
		<th nowrap><fmt:message key="contact.T_MainVer" /></th>
		<th nowrap><fmt:message key="contact.T_TaskVer" /></th>
		<th nowrap><fmt:message key="contact.T_FuncName" /></th>
		<th nowrap><fmt:message key="contact.T_FuncVer" /></th>
		<th nowrap><fmt:message key="contact.T_DISPLAY_NAME" /></th>
		<th nowrap><fmt:message key="contact.T_NUMBER" /></th>
    </tr>
</thead>
<tbody>

<%  
    Collection<Contact> contacts = ContactManager.getInstance().getContactList(start, range);
    if (contacts.isEmpty()) {
%>
    <tr>
        <td align="center" colspan="9">
            <fmt:message key="contact.summary.not_contact" />
        </td>
    </tr>

<%
    }
    int i = start;
    for (Contact contact : contacts) {
        i++;
%>
    <tr class="jive-<%= (((i%2)==0) ? "even" : "odd") %>">
        <td width="1%"><%= i %></td>
        <td ><%= contact.getUsername() %></td>
        <td ><%= contact.getT_DateTime(true) %></td>
        <td ><%= contact.getT_MainVer() %></td>
        <td ><%= contact.getT_TaskVer() %></td>
        <td ><%= contact.getT_FuncName() %></td>
        <td ><%= contact.getT_FuncVer() %></td>
        <td ><%= contact.getT_DISPLAY_NAME() %></td>
        <td ><%= contact.getT_NUMBER() %></td>
    </tr>

<%
    }
%>
</tbody>
</table>
</div>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="contact-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="contact-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="contact-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

    </body>
</html>
